/*
 * Copyright (C) 2010 Daniel Sundberg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nu.danielsundberg.droid.spinbox.math;

/**
 * Represents a vector
 * 
 * @author Daniel Sundberg
 *
 */
public class Vector4f {

	Vertice3f a;
	float length;
	
	public Vector4f(Vertice3f a, float length) {
		this.a = a;
		this.length = length;
	}
	
	private Vertice3f reduceToUnit(Vertice3f vector) {  
		// Reduces A Normal Vector (3 Coordinates)
		// To A Unit Normal Vector With A Length Of One.
		// Calculates The Length Of The Vector
		float length = (float) Math.sqrt((vector.x * vector.x) + (vector.y * 
				vector.y) + (vector.z * vector.z));

		if (length == 0.0f)  // Prevents Divide By 0 Error By Providing
			length = 1.0f;  // An Acceptable Value For Vectors To Close To 0.

		vector.x /= length;  // Dividing Each Element By
		vector.y /= length;  // The Length Results In A
		vector.z /= length;  // Unit Normal Vector.
		
		return vector;
	}
	
	// Calculates Normal For A Quad Using 3 Points
	private Vertice3f calcNormal(Vertice3f p0, Vertice3f p1, Vertice3f p2) {

		// Finds The Vector Between 2 Points By Subtracting
		// The x,y,z Coordinates From One Point To Another.

		// Calculate The Vector From Point 1 To Point 0
		Vertice3f tmp1 = new Vertice3f(
				p0.x - p1.x,
				p0.y - p1.y,
				p0.z - p1.z);  // Vector 1.x=Vertex[0].x-Vertex[1].x
		
		// Calculate The Vector From Point 2 To Point 1
		Vertice3f tmp2 = new Vertice3f(
				p1.x - p2.x,  // Vector 2.x=Vertex[0].x-Vertex[1].x
				p1.y - p2.y,  // Vector 2.y=Vertex[0].y-Vertex[1].y
				p1.z - p2.z);  // Vector 2.z=Vertex[0].z-Vertex[1].z
		
		// Compute The Cross Product To Give Us A Surface Normal
		Vertice3f normal = new Vertice3f(
				p1.y * tmp2.z - tmp1.z * tmp2.y,  // Cross Product For Y - Z
				p1.z * tmp2.x - tmp1.x * tmp2.z,  // Cross Product For X - Z
				p1.x * tmp2.y - tmp1.y * tmp2.x);  // Cross Product For X - Y

		return reduceToUnit(normal); // Normalize The Vectors


	}
	
}
